//
//  DropDownButton.m
//  etionLib
//
//  Created by wu jingxing on 12-6-8.
//  Copyright (c) 2012年 GuangZhouXuanWu. All rights reserved.
//

#import "DropDownButton.h"

//#ifdef ETIONIPAD
//@implementation CPopoverController 
//
//@synthesize m_btn;
//
//-(void)dealloc
//{
//    [super dealloc];
//}
//
//@end
//#endif

@implementation CDropDownButton
{
    CGFloat _rightSpace;
    CGFloat _leftSpace;
    
    //#ifdef ETIONIPAD
    UIPopoverController* _popver;
    UIPopoverArrowDirection _arrowdirection;
    //#endif
    
    __unsafe_unretained id<CDropDownButtonDelegate> _delegate;
}

@synthesize leftSpace = _leftSpace;
@synthesize rightSpace = _rightSpace;
@synthesize delegate = _delegate;
@synthesize fontColor = _fontColor;

//#ifdef ETIONIPAD
@synthesize popver = _popver;
@synthesize arrowdirection = _arrowdirection;
//#endif

@synthesize szTag;

-(void)awakeFromNib
{
    [super awakeFromNib];

    [self setupWithVcl:nil];
}

- (id)initWithFrame:(CGRect)frame vcl:(UIViewController *)vcl
{
    self = [super initWithFrame:frame];

    [self setupWithVcl:vcl];
    
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    return [self initWithFrame:frame vcl:nil];
}

-(void)setupWithVcl:(UIViewController *)vcl
{
    _rightSpace = 20;
    _leftSpace = 10;
    
    self.font = [UIFont systemFontOfSize:16];
    self.fontColor = [UIColor blackColor];
    self.backgroundImage = [[UIImage imageNamed:@"ddbtn_bg_normal.png"] resizeableCenterImage];
    
    UIImage* arrowImage=[UIImage imageNamed:@"ddbtn_button_arrow.png"];
    CGSize size = CGSizeMake(MIN(self.height,arrowImage.size.width),MIN(self.height,arrowImage.size.height));
    UIImageView *arrowImageView = [[UIImageView alloc] initWithImage:arrowImage];
    arrowImageView.frame = CGRectMake(self.width - size.width - 5, (self.height - size.height) / 2, size.width, size.height);
    [self addSubview:arrowImageView];
    [arrowImageView release];
    
    [super addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //#ifdef ETIONIPAD
    if (vcl != nil)
    {
        _popver = [[UIPopoverController alloc] initWithContentViewController:vcl];
        _popver.delegate = self;
        _arrowdirection = UIPopoverArrowDirectionUp;
    }
    //#endif
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
    return CGRectMake(_leftSpace, 5, CGRectGetWidth(contentRect) - _leftSpace - _rightSpace, CGRectGetHeight(contentRect) - 10);
}

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
{

}

- (void)setTitle:(NSString *)title
{
    [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self setTitle:title forState:UIControlStateNormal];
}

- (NSString *)title
{
    return [self titleForState:UIControlStateNormal];
}

- (void)setBackgroundImage:(UIImage *)backgroundImage
{
    [self setBackgroundImage:backgroundImage forState:UIControlStateNormal];
}

- (UIImage *)backgroundImage
{
    return [self backgroundImageForState:UIControlStateNormal];
}

- (void)setFont:(UIFont *)font
{
    self.titleLabel.font = font;
}

- (UIFont *)font
{
    return self.titleLabel.font;
}

- (void)setFontColor:(UIColor *)fontColor
{
    [self setTitleColor:fontColor forState:UIControlStateNormal];
}

- (UIColor *)fontColor
{
    return [self titleColorForState:UIControlStateNormal];
}

- (void)btnClick:(CDropDownButton *)sender
{
    if (ISIPHONE == YES)
    {
        if ([_delegate respondsToSelector:@selector(dropDownButtonClick:)] == YES)
            [_delegate dropDownButtonClick:self];
    }
    else
    {
//#ifdef ETIONIPAD
        BOOL bResult = NO;
        if ([_delegate respondsToSelector:@selector(popoverControllerShouldDelayShowPopover:popover:)] == YES)
            bResult = [_delegate popoverControllerShouldDelayShowPopover:self popover:_popver];
        if (bResult == YES)
            return;
        [self presentPopover];
//#endif
    }
}

//#ifdef ETIONIPAD
- (void)setPopver:(UIPopoverController *)popver
{
    [_popver release];
    _popver = [popver retain];
    _popver.delegate = self;
}

- (void)setPopoverViewController:(UIViewController *)vcl
{
    if (vcl == nil)
    {
        _popver.contentViewController = nil;
        return;
    }
    if (_popver == nil)
    {
        _popver = [[UIPopoverController alloc] initWithContentViewController:vcl];
        _popver.delegate = self;
        _arrowdirection = UIPopoverArrowDirectionUp;
    }
    _popver.contentViewController = vcl;
    _popver.popoverContentSize = vcl.view.frame.size;
    
    if([vcl respondsToSelector:@selector(setDropDownButton:)])
        [(id<CDropDownViewControllerDelegate>)vcl setDropDownButton:self];
        
}

- (void)presentPopover
{
    if ([_delegate respondsToSelector:@selector(popoverControllerWillShowPopover:popover:)] == YES)
        [_delegate popoverControllerWillShowPopover:self popover:_popver];
    [_popver presentPopoverFromRect:self.frame inView:self.superview permittedArrowDirections:_arrowdirection animated:YES];
}

- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController
{
    BOOL bResult = YES;

    if ([_delegate respondsToSelector:@selector(popoverControllerShouldDismissPopoverpopover:)] == YES)
        bResult = [_delegate popoverControllerShouldDismissPopover:self popover:popoverController];

    return bResult;
}

- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
    if ([_delegate respondsToSelector:@selector(popoverControllerDidDismissPopoverpopover:)] == YES)
        [_delegate popoverControllerDidDismissPopover:self popover:popoverController];
}

- (void)dismissPopover
{
    [_popver dismissPopoverAnimated:YES];
    if ([_delegate respondsToSelector:@selector(popoverControllerDidDismissPopoverpopover:)] == YES)
        [_delegate popoverControllerDidDismissPopover:self popover:_popver];
}
//#endif

- (void)reset
{
    self.title = @"";
    if ([_delegate respondsToSelector:@selector(dropDownButtonReset:)] == YES)
        [_delegate dropDownButtonReset:self];
}

-(void)dropDown
{
    [self btnClick:self];
}

- (void)dealloc
{
//#ifdef ETIONIPAD
    [_popver release_mm];
//#endif
    self.szTag = nil;
    superdealloc_mm;
}

@end
